package com.example.demo.service.serviceImpl;

import com.example.demo.pojo.Account;
import com.example.demo.repository.AccountRepository;
import com.example.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public boolean addAccount(Account account) {

        account.setDelete(false);
        Account account1 = accountRepository.findAccountsByUsername(account.getUsername());
        if (account1 != null) {
            return false;
        }
        Account save = accountRepository.save(account);
        if (save != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean searchAccountByUserNameAndPassword(String username, String password) {

        Account account = accountRepository.findAccountByUsernameAndPassword(username, password);
        if (account != null) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void updatePasswordByUserName(String password, String username) {

        accountRepository.updatePasswordByUsername(password, username);
        return ;
    }

    @Override
    public boolean searchAccountByUserNameAndEmail(String username, String email) {

        Account accounts = accountRepository.findAccountByUsernameAndEmail(username, email);
        if(accounts == null) {
            return false;
        } else {
            return true;
        }
    }
}
